<?php
defined('IN_PHPCMS') or exit('No permission resources.');
class guestbook
{
  // protected $guestbook, $siteid, $format;
  function __construct() {
    // 加载留言本的数据模型
    $this->db = pc_base::load_model('guestbook_model');

    // 取得当前登录会员的会员名(username)和会员ID(userid)
    // $this->_username = param::get_cookie('_username');
    // $this->_userid = param::get_cookie('_userid');

    //定义站点ID常量，选择模版使用
    $siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();
    define("SITEID", $siteid);

    //读取配置
    $setting = new_html_special_chars(getcache('guestbook', 'commons'));
    $this->set = $setting[SITEID];
  }

  public function init() {
    $num = serial_number();
    // $str = 'MF000'.','.$num;
    // $arr = explode(',',$str);

    var_dump(count($_POST));
    // var_dump($s[0]*1000);
    // var_dump($arr][0]);
    // $where .= " `ip`='".$_SERVER["REMOTE_ADDR"]."' AND `addtime` > (".time()."-60)";
    // var_dump($where);
    // $r = $this->db->get_one($where);
    // var_dump($r);
    // exit();
    //设置分页条数
    // $pagesize = $this->set['pagesize'];
    //
    // $where = array('passed'=>1,'siteid'=>SITEID);
    // $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
    // $infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, $pagesize);
    // $infos = new_html_special_chars($infos);

    // 加载系统form类，用于前台模块文件中生成验证码
    // pc_base::load_sys_class('form', '', 0);

    // 加载前台模板
    // include template('guestbook', 'index');
  }

  public function post() {
    try {
      //提交数据全部为空
      if (count($_POST) <= 0) die('MF001');

      // 标题和内容为空
      if ( !$this->checkField(['title','content']) ) die('MF002');



      //同一ip提交留言限制60秒
      $where .= " `ip`='".$_SERVER["REMOTE_ADDR"]."' AND `addtime` > (".time()."-60)";
      $result = $this->db->get_one($where);
      if ($result) { die('MF004'); }

      /*V9验证码的数值是通过SESSION传递，故在这段代码中，首先加载配置文件，
       取出当前系统配置中SESSION的存储方式。然后根据SESSION的存储方式，来加载对应的系统类库*/
      $session_storage = 'session_'.pc_base::load_config('system','session_storage');
      pc_base::load_sys_class($session_storage);
      if(!isset($_SESSION)) session_start();

      // 验证码不存在
      if(!isset($_POST['code'])) die('MF003');
      if ($_SESSION['code'] != strtolower(trim($_POST['code']))) die('MF003');

      $num = serial_number();
      $data['siteid'] = SITEID;
      $data['username'] = $_POST['username'];
      $data['email'] = $_POST['email'];
      $data['phone'] = $_POST['phone'];
      $data['address'] = $_POST['address'];
      $data['title'] = $_POST['title'];
      $data['content'] = $_POST['content'];
      $data['ip'] = $_SERVER["REMOTE_ADDR"];
      $data['addtime'] = time();
      $data['replyid'] = $num;

      $this->db->insert($data);
      die('MF000,'.$num);
    }
    catch (Exception $e) {
      die('MF255');
    }
  }//post end

  /**
	 * 留言查询函数
	 * @return [view] search/reply
	 */
	public function search_reply(){
		//获取siteid
		$siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
		$SEO = seo($siteid);
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');

		if ( isset($_GET['q']) && trim($_GET['q'])!='' ) {
			#查询
			$q = safe_replace(trim($_GET['q']));
			$q = new_html_special_chars(strip_tags($q));
			$q = str_replace('%', '', $q);	//过滤'%'，用户全文搜索

			$data = $this->db->get_one("`replyid` = '$q'");
		}
		include	template('search','reply');
	}

  /**
   * 字段为空检查
   * @param  [type] $fields [description]
   * @return [type]         [description]
   */
  protected function checkField($fields) {
    foreach ($fields as $key => $value) {
      if(!isset($_POST[$value]) or trim($_POST[$value])=='') return false;
    }
    return true;
  }


}
?>
